<?
if(!defined ('U_MOD'))
{
	echo"<meta http-equiv='refresh' content='0;url=/index.php'>";
	exit;
}

include 'topmenu.php';
$showForm=1;

if (!isset($FormError))
{
	$i=0;
	$FormError=array();
}

if (isset($act) && ($act==1 || $act==2))
{
	if ($act==1){$ActionUrl=1;  $ActionButton=_ADDNEW_;}
	if ($act==2){$ActionUrl=2;  $ActionButton=_EDITE_; $midActionUrl='&nid='.$nid;}

	if ($act==1 || $act==2)
	{
		$queryNavCheck=mysql_query("SELECT nav_id FROM ".$PortalPref.$amk." WHERE nav_id='".$nid."'");

		if(mysql_num_rows($queryNavCheck)==0 && $act==2)
		{
			$FormError[$i]=_NO_NAVIGATION_FOR_EDITE_;
			$i++;
			$showForm=0;
			echo"<meta http-equiv='refresh' content='2;url=/admin.php?amk=".$amk."'>";
		}else
		{
			if ($act==2 && (!isset($nid) || !is_numeric($nid)))
			{
				$FormError[$i]=_NO_NAVIGATION_FOR_EDITE_;
				$i++;
				$showForm=0;
				echo"<meta http-equiv='refresh' content='2;url=/admin.php?amk=".$amk."'>";
			}
			if (!isset($ntitle) || $ntitle=='')
			{
				$FormError[$i]=_NO_NAVIGATION_TITLE_;
				$i++;
			}else
				$ntitle=trim($ntitle);
			if (!isset($nparent) || !is_numeric($nparent) || $nparent<0)
			{
				$FormError[$i]=_NO_PARENT_KEY_;
				$i++;
			}
			if (!isset($mkeyvisible) || is_numeric($mkeyvisible) || $mkeyvisible<0 || $mkeyvisible=='')
			{
				$FormError[$i]=_NO_VISIBILITY_KEY_;
				$i++;
			}
			if (!isset($ntarget) || !is_numeric($ntarget) || $ntarget<0)
			{
				$FormError[$i]=_NO_TARGET_KEY_;
				$i++;
			}
			if (!isset($mkey) || is_numeric($mkey) || $mkey<0 || $mkey=='')
			{
				$FormError[$i]=_NO_MODULE_KEY_;
				$i++;
			}
			if (isset($ntarget) && $ntarget==2)
			{
				if (!isset($npage) || $npage=='')
				{
					$FormError[$i]=_NO_NAVIGATION_PAGE_;
					$i++;
				}else
					$npage=trim($npage);
			}
			if (!isset($npagepose) || is_numeric($npagepose) || $npagepose<0 || $npagepose=='')
			{
				$FormError[$i]=_NO_PAGEPOSITION_KEY_;
				$i++;
			}
			if (!isset($nright) || !is_numeric($nright) || $nright<0)
			{
				$FormError[$i]=_NO_RIGHT_KEY_;
				$i++;
			}

			if (empty($FormError))
			{
				if (!isset($nfooter) || empty($nfooter) || $nfooter=='')
					$n_footer=0;
				else
					$n_footer=1;

				if (!isset($nactivity) || empty($nactivity) || $nactivity=='')
					$n_activity=0;
				else
					$n_activity=1;

				if ($act==1)
				{
					if ($n_footer==1)
					{
						$queryGetMaxOrder=mysql_query("SELECT MAX(nav_footer) AS max FROM ".$PortalPref.$amk);
						$resultGetMaxOrder=mysql_fetch_assoc($queryGetMaxOrder);
						if(is_null($resultGetMaxOrder['max']))
							$orderFooter=1;
						else
							$orderFooter=$resultGetMaxOrder['max']+1;
					}else
						$orderFooter=0;

					$queryGetMaxOrder=mysql_query("SELECT MAX(nav_order) AS max FROM ".$PortalPref.$amk." WHERE nav_parent_id='".$nparent."'");
					$resultGetMaxOrder=mysql_fetch_assoc($queryGetMaxOrder);
					if(is_null($resultGetMaxOrder['max']))
						$orderChild=0;
					else
						$orderChild=$resultGetMaxOrder['max']+1;

					$queryNav="INSERT INTO ".$PortalPref.$amk."
						(
							nav_id,
							nav_title,
							nav_parent_id,
							nav_mkey,
							nav_admin,
							nav_module,
							nav_page,
							nav_page_position,
							nav_footer,
							nav_order,
							nav_right,
							nav_activity
						)VALUES
						(
							NULL,
							'".$ntitle."',
							'".$nparent."',
							'".$mkeyvisible."',
							'".$ntarget."',
							'".$mkey."',
							'".$npage."',
							'".$npagepose."',
							'".$orderFooter."',
							'".$orderChild."',
							'".$nright."',
							'".$n_activity."'
						)";
				}elseif($act==2)
				{
					if ($old_footer!=$n_footer)
					{
						if ($n_footer==1)
						{
							$queryGetMaxOrder=mysql_query("SELECT MAX(nav_footer) AS max FROM ".$PortalPref.$amk." WHERE nav_id <> '".$nid."'");
							$resultGetMaxOrder=mysql_fetch_assoc($queryGetMaxOrder);
							if(is_null($resultGetMaxOrder['max']))
								$orderFooter=1;
							else
								$orderFooter=$resultGetMaxOrder['max']+1;
						}else
						{
							$orderFooter=0;
							$queryOldFooterOrder=mysql_query("SELECT nav_footer FROM ".$PortalPref.$amk." WHERE nav_id ='".$nid."'");
							$resultOldFooterOrder=mysql_fetch_assoc($queryOldFooterOrder);

							$queryNewFooterOrder="UPDATE ".$PortalPref.$amk." SET nav_footer=nav_footer-1 WHERE nav_footer > '".$resultOldFooterOrder['nav_footer']."'";
						}
						$queryAddFooterOrder="nav_footer='".$orderFooter."',";
						$doNavFooterQuery=true;
					}else
					{
						$doNavFooterQuery=false;
						$queryAddFooterOrder="";
					}

					if($old_nparent!=$nparent)
					{
						$queryGetMaxOrder=mysql_query("SELECT MAX(nav_order) AS max FROM ".$PortalPref.$amk." WHERE nav_parent_id='".$nparent."' AND nav_id <> '".$nid."'");
						$resultGetMaxOrder=mysql_fetch_assoc($queryGetMaxOrder);
						if(is_null($resultGetMaxOrder['max']))
							$orderChild=0;
						else
							$orderChild=$resultGetMaxOrder['max']+1;

						$queryAddOrder="nav_order='".$orderChild."',";
					}else
						$queryAddOrder="";

					$queryNav="UPDATE ".$PortalPref.$amk."
					SET
						nav_title='".$ntitle."',
						nav_parent_id='".$nparent."',
						nav_mkey='".$mkeyvisible."',
						nav_admin='".$ntarget."',
						nav_module='".$mkey."',
						nav_page='".$npage."',
						nav_page_position ='".$npagepose."',
						".$queryAddFooterOrder."
						".$queryAddOrder."
						nav_right='".$nright."',
						nav_activity='".$n_activity."'
					WHERE
						nav_id='".$nid."'
					";
				}
				if (mysql_query($queryNav))
				{
					if ($act==2 && $n_footer==0 && $doNavFooterQuery)
						mysql_query($queryNewFooterOrder);
					$showForm=0;
					print '<p align="center">'._OPERATION_SUCCESS_.'</p>';
					echo"<meta http-equiv='refresh' content='0;url=/admin.php?amk=".$amk."'>";
				}else
				{
					$FormError[$i]=_BAD_SQL_;
					$i++;
				}
			}
		}
	}
}elseif(isset($act) && $act==3)
{
	$ActionUrl=2;  $ActionButton=_EDITE_;
	if(!isset($nid))
	{
		$FormError[$i]=_NO_NAVIGATION_FOR_EDITE_;
		$i++;
		$showForm=0;
		echo"<meta http-equiv='refresh' content='2;url=/admin.php?amk=".$amk."'>";
	}else
	{
		$queryNavSelect=mysql_query("SELECT * FROM ".$PortalPref.$amk." WHERE nav_id='".$nid."'");
		if (mysql_num_rows($queryNavSelect)!=1)
		{
			$FormError[$i]=_NO_NAVIGATION_FOR_EDITE_;
			$i++;
			$showForm=0;
			echo"<meta http-equiv='refresh' content='2;url=/admin.php?amk=".$amk."'>";
		}else
		{
			while($resultNavSelect=mysql_fetch_assoc($queryNavSelect))
			{
				$ntitle=$resultNavSelect['nav_title'];
				$nparent=$resultNavSelect['nav_parent_id'];
				$mkeyvisible=$resultNavSelect['nav_mkey'];
				$ntarget=$resultNavSelect['nav_admin'];
				$mkey=$resultNavSelect['nav_module'];
				$npage=$resultNavSelect['nav_page'];
				$npagepose=$resultNavSelect['nav_page_position'];
				$nfooterOld=$resultNavSelect['nav_footer'];
				if($nfooterOld>0)
					$nfooter=1;
				else
					$nfooter=$nfooterOld;

				$nright=$resultNavSelect['nav_right'];
				$nactivity=$resultNavSelect['nav_activity'];
			}
			$midActionUrl='&nid='.$nid;
		}
	}
}else
{
	$ActionUrl=1;  $ActionButton=_ADDNEW_;
}

print '<p>';
getErrorList($FormError);
print '</p>';

if (isset($nfooter) && $nfooter==1)
	$nfooter_flag='checked';
else
	$nfooter_flag='';

if (isset($nactivity) && $nactivity==1)
	$nactivity_flag='checked';
else
	$nactivity_flag='';

if ($showForm==1)
{
	$queryNavMainForManage=mysql_query("SELECT * FROM ".$PortalPref.$amk." ORDER BY nav_order ASC");
	$MenuArrayForManage = getMenuArrayAdmin($queryNavMainForManage);

	print '<form method="post" name="ModuleFrm" action="'.$portal_url.'/admin.php?amk='.$amk.'&apk='.$apk.'&act='.$ActionUrl.''.$midActionUrl.'">
		<table width="80%" align="center">
			<tr>
				<td>'._NAVIGATION_TITLE_.'</td>
				<td><input type="text" name="ntitle" value="'.$ntitle.'"></td>
			</tr>
			<tr>
				<td>'._NAVIGATION_PARENT_.'</td>
				<td>';
					if ($ActionUrl==2)
					{
						if ($act==3)
							$old_nparent=$nparent;
						elseif($act==2)
							$old_nparent=$old_nparent;
						print '<input type="hidden" name="old_nparent" value="'.$old_nparent.'">';
					}

					$selectedRoot='';
					if ($nparent==0)
						$selectedRoot='selected';
					print'<select name="nparent">
						<option value="-1">'._SELECT_PARENT_.'</option>
						<option value="0" '.$selectedRoot.'>'._ROOT_POSITION_.'</option>
						'.getMenuTreeForManage($MenuArrayForManage, 0, 0, $nparent).'
					</select>
				</td>
			</tr>';
			if (!isset($mkeyvisible) || empty($mkeyvisible) || $mkeyvisible=='' || is_numeric($mkeyvisible))
				$VisibleModuleSelected='portal';
			else
				$VisibleModuleSelected=$mkeyvisible;

			print '<tr>
				<td>'._NAVIGATION_VISIBILITY_.'</td>
				<td>';
					GetModuleList($VisibleModuleSelected,0,'mkeyvisible',_IN_ALL_,'','');
				print'</td>
			</tr>';
			if (!isset($ntarget))
				$ntarget=-1;
			print'<tr>
				<td>'._NAVIGATION_TARGET_.'</td>
				<td>
					<select name="ntarget">
						'.GetNavTarget($ntarget).'
					</select>
				</td>
			</tr>';
			if (!isset($mkey) || empty($mkey) || $mkey=='' || is_numeric($mkey))
				$moduleSelected='portal';
			else
				$moduleSelected=$mkey;

			print '<tr>
				<td>'._MODULE_.'</td>
				<td>';
					GetModuleList($moduleSelected,0,'mkey',_FOR_PORTAL_,'','');
				print'</td>
			</tr>';
			print'<tr>
				<td>'._NAVIGATION_PAGE_.'</td>
				<td><input type="text" name="npage" value="'.$npage.'"></td>
			</tr>
			<tr>
				<td>'._NAVIGATION_PAGE_POSITION_.'</td>
				<td>
					<select name="npagepose">
						'.GetNavPosition($npagepose).'
					</select>
				</td>
			</tr>
			<tr>
				<td>'._NAVIGATION_FOOTER_.'</td>
				<td>';
					if ($ActionUrl==2)
					{
						if ($act==3)
							$old_footer=$nfooter;
						elseif($act==2)
							$old_footer=$old_footer;

						print '<input type="hidden" name="old_footer" value="'.$old_footer.'">';
					}
					print'<input type="checkbox" name="nfooter" value="1" '.$nfooter_flag.'>
				</td>
			</tr>
			<tr>
				<td>'._RIGHT_LEVEL_.'</td>
				<td>
					<select name="nright">
						<option value="-1">'._SELECT_RIGHT_.'</option>
						'.GetRightList($nright).'
					</select>
				</td>
			</tr>
			<tr>
				<td>'._NAVIGATION_ACTIVITY_.'</td>
				<td><input type="checkbox" name="nactivity" value="1" '.$nactivity_flag.'></td>
			</tr>
			<tr>
				<td colspan="2"><input type="submit" value="'.$ActionButton.'"></td>
			</tr>
		</table>
	</form>';
}


?>